package com.shanghaionstar.dealersorder;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.GroundOverlayOptions;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.dealer.MaintenanceDealerResponseEntity;
import com.onstar.cn.map.OMapView;
import com.onstar.cn.weight.TopLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.swipe.SwipeActivity;

/**
 * Created by Yonglang Hong on 2016/8/12.
 */
@ContentView(R.layout.onstar_dealer_showmap)
public class DealerPoiMapActivity extends SwipeActivity {

    @FindViewById
    private TopLayout top_layout;
    private View view;
    private TextView text_dealer_info;
    private TextView text_dealer_space_info;
    private TextView text_dealer_address_info;
    private Button btn_booking;
    private Button btn_to_booking_list;
    private LinearLayout linear_showmap;
    @FindViewById
    private OMapView mapView;
    @FindViewById
    ProgressBar progress;
    private AMap mAMap;
    private DisplayMetrics dm;
    private Marker mark;
    private MaintenanceDealerResponseEntity dealerBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(savedInstanceState);
    }

    private void init(Bundle savedInstanceState) {
        initMap(savedInstanceState);
        showInfoOnMap();
    }

    private void initMap(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);// 必须设置才能初始化地图
        mAMap = mapView.getMap();
    }

    private void showInfoOnMap() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            MaintenanceDealerResponseEntity dealerBean = (MaintenanceDealerResponseEntity) bundle.getSerializable("dealerResponse");
            showMark(dealerBean);
            showDealerInfo(dealerBean);
        }
    }

    private void showMark(MaintenanceDealerResponseEntity dealerBean) {
        mAMap.clear();
        if (!CommonUtil.isNull(dealerBean.getServiceLat()) && !CommonUtil.isNull(dealerBean.getServiceLng())
                && !(Double.parseDouble(dealerBean.getServiceLat()) == 0) && !(Double.parseDouble(dealerBean.getServiceLng()) == -1)) {
            mAMap.moveCamera(CameraUpdateFactory
                    .newLatLngZoom(new LatLng(Double.parseDouble(dealerBean.getServiceLat()), Double.parseDouble(dealerBean.getServiceLng())), 12));
            // 不添加bounds位置
            GroundOverlayOptions groundoverlay = new GroundOverlayOptions();
            //设置 ground 覆盖物的锚点比例，默认为 0.5f，水平和垂直方向都居中对齐
            groundoverlay.anchor(0.5f, 0.5f);
            // 设置 ground 覆盖物透明度
            groundoverlay.transparency(0.1f);
            LatLng point = new LatLng(Double.parseDouble(dealerBean.getServiceLat()), Double.parseDouble(dealerBean.getServiceLng()));
            // 构建Marker图标
            BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_geofence_location);
            // 构建MarkerOption，用于在地图上添加Marker
            MarkerOptions option = new MarkerOptions();
            //设置标记点的坐标信息
            option.position(point);
            //设置标记点显示的图片
            option.icon(bitmap);
            // 在地图上添加Marker
            mark = mAMap.addMarker(option);
            // 显示窗口
            mark.showInfoWindow();
        }
    }

    private void showDealerInfo(final MaintenanceDealerResponseEntity dealerBean) {
        if (linear_showmap != null)
            linear_showmap.removeAllViews();
        linear_showmap = (LinearLayout) findViewById(R.id.dealer_address_dialog_bottom);
        view = LayoutInflater.from(this).inflate(R.layout.onstar_dealer_booking_dialog, null);
        text_dealer_info = (TextView) view.findViewById(R.id.text_dealer_info);
        text_dealer_space_info = (TextView) view.findViewById(R.id.text_dealer_space_info);
        text_dealer_address_info = (TextView) view.findViewById(R.id.text_dealer_address_info);
        btn_booking = (Button) view.findViewById(R.id.btn_booking);
        btn_to_booking_list = (Button) view.findViewById(R.id.btn_to_booking_list);
        text_dealer_info.setText(dealerBean.getName());
        text_dealer_space_info.setText(dealerBean.getDistance() + getString(R.string.geofence_range_unit_km));
        text_dealer_address_info.setText(dealerBean.getServiceAddress());
        //立即预约——跳转到预约信息页面
        btn_booking.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                BusinessUtil.sendReportByClickType(DealerPoiMapActivity.this, FunctionNameFindId.Dealer_Map_Booking.getStringValue());
                Intent intent = new Intent(DealerPoiMapActivity.this, OrderActivity.class);
                Bundle args = new Bundle();
                args.putSerializable("dealerResponse", dealerBean);
                intent.putExtras(args);
                startActivity(intent);
                finish();
            }
        });

        //返回——经销商列表页面
        btn_to_booking_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
//                BusinessUtil.sendReportByClickType(DealerPoiMapActivity.this, FunctionNameFindId.Dealer_Map_List.getStringValue());
            }
        });
        // 获取屏幕像素相关信息
        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        // 根据屏幕信息设置View容器的宽高
        view.setLayoutParams(new ViewGroup.LayoutParams(dm.widthPixels * 9 / 10, dm.heightPixels / 4));
        // 将View容器设置到布局文件父容器中
        linear_showmap.addView(view);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapView != null) {
            mapView.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mapView != null) {
            mapView.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapView != null) {
            mapView.onDestroy();
        }
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }
}
